﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UtilityExtensions;
using BOL;
using DAL;
using System.Data.SqlClient;
using Website.Logger;

namespace UILGestion.Control
{
    public partial class ECItemAddOCPanel : System.Web.UI.UserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
         }

        public int boolEsReAgrupado
        {

            get
            {
                return EsReAgrupado.Value.ToInt();
            }

            set
            {

                EsReAgrupado.Value = value.ToString();
         
            }
            }

        public void LoadFromOC(int idEstructuraCostos,int idMoneda, string idListYaUtilizado, string orden = default(string))
        {
            try
            {

     
                List<EstructuraCostoitemsingleInfo> ds = (List<EstructuraCostoitemsingleInfo>)System.Web.HttpContext.Current.Session[idListYaUtilizado];
               
                List<Int32> itemYaUtilizado = ds.Select(p => p.idOrdenCompraItem).ToList();

                itemGridView.DataSource = new OrdenCompraBL().getOrdenCompraSinComparativo(idEstructuraCostos, itemYaUtilizado);
                itemGridView.DataBind();
                UpdatePanelGrilla.Update();
                
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }



        protected void ItemAllSelectChange(object sender, EventArgs e)
        {
            //solo los seleccionados
            //seleccionamos todos los  item
            foreach (GridViewRow row in itemGridView.Rows)
            {
                CheckBox cb = (CheckBox)(row.FindControl("chkEmployeeSelector"));
                cb.Checked = ((CheckBox)sender).Checked;
            }
            UpdatePanelGrilla.Update();
           // setEstadistica();

        }
        protected void ItemSelectChange(object sender, EventArgs e)
        {
            //solo los seleccionados
            //seleccionamos todos los  item

            //setEstadistica();

        }
        protected string GetSortDirection(string column)
        {

            // By default, set the sort direction to ascending.
            string sortDirection = "ASC";

            // Retrieve the last column that was sorted.

            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                // Check if the same column is being sorted.
                // Otherwise, the default value can be returned.
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }

            // Save new values in ViewState.
            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }

        protected void gridview_sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {

                string orden = e.SortExpression + " " + GetSortDirection(e.SortExpression);

               // LoadRecurso(int.Parse(MonedaDropDown.SelectedValue), idListYaUtilizados.Value, orden);

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }







        }
        public List<EstructuraCostoitemsingleInfo> GetSeleccionList(int idMoneda,
                                                            decimal tipoCambioOBRADOL,
                                                            string simbolDDL,
                                                            string simbolMonedaBase)
        {

            // Create a List to hold the EmployeeID values to delete
            EstructuraCostoBL bol = new EstructuraCostoBL();
            List<EstructuraCostoitemsingleInfo> ListaRecurso = new List<EstructuraCostoitemsingleInfo>();

            // Iterate through the Employees.Rows property
            foreach (GridViewRow row in itemGridView.Rows)
            {
                // Access the CheckBox
                CheckBox cb = (CheckBox)(row.FindControl("chkEmployeeSelector"));
                if (cb != null && cb.Checked)
                {
                    // First, get the ordenCompraID for the selected row
                    Int32 idInternal = (Int32)itemGridView.DataKeys[row.RowIndex].Value;

                    ListaRecurso.Add(bol.GetFromOCItem(idInternal, 
                                                        idMoneda, 
                                                        tipoCambioOBRADOL, 
                                                        simbolDDL,
                                                        simbolMonedaBase));
                }
            }

            return ListaRecurso;

        }
        
        
          
       
    }
}